package com.zch.socket.socket.client;

import java.io.*;
import java.net.Socket;

public class ClientTask  {
    private Socket socket;
    private String host;
    private int port;

    public ClientTask(String host, int port) throws IOException {
        this.host = host;
        this.port = port;
        this.socket=new Socket(host,port);
    }

    public boolean sendMsg(String msg) throws Exception {
        // 客户端发送消息，相对自己而言是输出。用输出流
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream=socket.getInputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
        ObjectInputStream objectInputStream=new ObjectInputStream(inputStream);
        objectOutputStream.writeObject(msg);
        String str= (String) objectInputStream.readObject();
        System.out.println("收到服务端返回"+str);
        inputStream.close();
        outputStream.close();
        return true;
    }
}
